<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\common\helper\util\Page;
use app\common\helper\util\Tool;
use app\exam\model\admin\Module as ModuleModel;
use app\exam\validate\admin\Module as ModuleValidate;
use think\Model;

/**
 * Class Module
 *
 * @package app\exam\logic\admin
 */
class Module extends ModuleModel
{
    /**
     * 获取状态选项值
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/25
     *
     * @return array
     */
    public function getStatusOptions()
    {
        $options = $this->getStatusList();

        return Tool::parseToSelectOpions($options);
    }

    /**
     * 获取版块属性
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param bool $unlimit 需要不限制选项
     * @return array[]
     */
    public function getModuleTypeOptions(bool $unlimit = true)
    {
        $options = $this->getModuleType();

        return Tool::parseToSelectOpions($options, $unlimit);
    }

    /**
     * 获取列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/25
     *
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map)
    {
        return $this->where($map)->paginate(Page::listRows())->append(['module_type_text']);
    }

    /**
     * 新建版块
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param array $data
     */
    public function createModule(array $data)
    {
        // 校验数据
        validate(ModuleValidate::class)->scene('create')->check($data);
        // 组装数据
        $save = [
            'title'            => $data['title'],
            'cover'            => $data['cover'],
            'module_type'      => $data['module_type'],
            // 'category'         => $data['category']
        ];

        if (!$this->save($save)) {
            throw new LogicException('新建失败，请重试');
        }

        Log::write('新建版块【' . $data['title'] . '】', 'exam', 'create', true);
    }

    /**
     * 编辑版块
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int   $id
     * @param array $data
     * @return bool
     */
    public function editModule(int $id, array $data): bool
    {
        $module     = $this->getModuleById($id);
        $data['id'] = $id;
        // 类型不可更改
        $data['module_type'] = $module->getAttr('module_type');
        // 验证数据
        validate(ModuleValidate::class)->scene('edit')->check($data);

        $module->title = $data['title'];
        $module->cover = $data['cover'];

        if (!$module->save()) {
            throw new LogicException('更新失败');
        }

        Log::write('更新版块【' . $data['title'] . '】', 'exam', 'update', true);

        return true;
    }

    /**
     * 根据ID获取版块信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int $id
     * @return Model
     */
    public function getModuleById(int $id): Model
    {
        return $this->with(['coverUrl'])->findOrFail($id);
    }

    /**
     * 更新单个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int    $id
     * @param string $field
     * @param        $value
     * @return Model
     */
    public function editField(int $id, string $field, $value)
    {
        // 信息
        $module = $this->getModuleById($id);

        $module->$field = $value;

        if (!$module->save()) {
            throw new LogicException("更新失败");
        }

        Log::write('更新版块【' . $module->title . '】', 'exam', 'update', true);

        // 返回值
        return $module;
    }

    /**
     * 删除版块
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int $id
     */
    public function deleteModule(int $id)
    {
        $module = $this->getModuleById($id);

        if (!$module->delete()) {
            throw new LogicException('删除失败');
        }

        Log::write('删除版块【' . $module->title . '】', 'exam', 'delete', true);

    }
}